<?php

use App\Exceptions\InvalidRequestException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;

if (!function_exists('config_path')) {
    /**
     * Get the configuration path.
     *
     * @param  string  $path
     * @return string
     */
    function config_path($path = '')
    {
        return app()->configPath($path);
    }
}

if (!function_exists('throw_invalid_request_exception')) {
    function throw_invalid_request_exception($errcode, $data = [], $message = '')
    {
        $errcode = is_null($errcode) ? 'common-error' : $errcode;
        $error = config('errcode.' . $errcode);
        $message = empty($message) ? $error['message'] : $message;
        throw new InvalidRequestException($message, $error['statusCode'], $error['code'], $data);
    }
}


//获取今日次数
if (!function_exists('get_times_today')) {
    function get_times_today($user_id, $cache_prefix, $initial = 0)
    {
        $seconds = 86400;
        $cache_key = $cache_prefix . '_' . date('Ymd') . ':' . $user_id;
        $value = Cache::remember($cache_key, $seconds, function () use ($initial) {
            return $initial;
        });
        return $value;
    }
}

//设置今日次数
if (!function_exists('set_times_today')) {
    function set_times_today($user_id, $cache_prefix, $value)
    {
        $cache_key = $cache_prefix . '_' . date('Ymd') . ':' . $user_id;
        $result = Cache::put($cache_key, $value, 86400);
        return $result;
    }
}

//递增今日次数
if (!function_exists('increment_times_today')) {
    function increment_times_today($user_id, $cache_prefix)
    {
        $cache_key = $cache_prefix . '_' . date('Ymd') . ':' . $user_id;
        return Cache::increment($cache_key);
    }
}


//递减今日次数
if (!function_exists('decrement_times_today')) {
    function decrement_times_today($user_id, $cache_prefix)
    {
        $cache_key = $cache_prefix . '_' . date('Ymd') . ':' . $user_id;
        return Cache::decrement($cache_key);
    }
}

//判断是否为时间格式
if (!function_exists('is_datetime')) {
    function is_datetime($datetime)
    {
        if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/s', $datetime)) {
            return true;
        } else {
            return false;
        }
    }
}

//从字符串中找出数字
if (!function_exists('find_num_from_string')) {
    function find_num_from_string($str = '')
    {
        $str = trim($str);
        if (empty($str)) {
            return 0;
        }
        $reg = '/\d+/s'; //匹配数字的正则表达式
        preg_match_all($reg, $str, $result);
        if (is_array($result) && !empty($result) && !empty($result[0]) && !empty($result[0][0])) {
            return intval($result[0][0]);
        }
        return 0;
    }
}

/*
 * $content 文章内容
 * $order 要获取哪张图片，ALL所有图片，0第一张图片
*/
if (!function_exists('getImgs')) {
    function getImgs($content, $order = 'ALL')
    {
        $pattern = "/<img .*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";
        preg_match_all($pattern, $content, $match);
        if (isset($match[1]) && !empty($match[1])) {
            if ($order === 'ALL') {
                return $match[1];
            }
            if (is_numeric($order) && isset($match[1][$order])) {
                return $match[1][$order];
            }
        }
        return [];
    }
}

if (!function_exists('is_phone')) {
    function is_phone($phone)
    {
        try {
            $data = [
                'phone' => $phone
            ];
            $rules = [
                'phone' => 'required|phone:CN,mobile',
            ];
            Validator::make($data, $rules)->validate();
            return true;
        } catch (Throwable $e) {
            return false;
        }
    }
}


//判断是否为时间格式
if (!function_exists('is_datetime')) {
    function is_datetime($datetime)
    {
        if (preg_match('/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/s', $datetime)) {
            return true;
        } else {
            return false;
        }
    }
}
